package com.xzr.kutumanager.config;

import com.xzr.kutumanager.util.ConstVariable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author：徐志荣
 * @Date：2023/7/18 下午 5:10
 * <p>
 * 用户自定义拦截器
 */
@Component
public class LoginIntercept implements HandlerInterceptor {

    /**
     * @param request
     * @param response
     * @param handler
     * @return true: 表示用户已经登录,会继续访问目标方法
     * false : 表示未登录, 跳转到登录页面
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(ConstVariable.USER_SESSION_KEY) != null) {
            //表示登录
            return true;
        }
        response.sendRedirect("/login.html");
        return false;
    }
}
